package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.Checkgroup;
import com.itheima.entity.CheckgroupCheckitem;
import com.itheima.entity.SetmealCheckgroup;
import com.itheima.exception.BussinessException;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.mapper.SetmealCheckgroupMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.QueryPageBean;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
@Transactional
public class CheckgroupServiceImpl implements CheckgroupService {

    @Autowired
    private CheckgroupMapper checkgroupMapper;

    @Autowired
    private CheckGroupItemService checkGroupItemService;

    @Autowired
    private CheckItemService checkItemService;

    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;

    // 新增
    @Override
    public void add(Checkgroup checkgroup, Integer[] checkitemIds) {
        //添加检查组基本信息

        checkgroupMapper.insert(checkgroup);
        //获取检查组id
        Integer checkgroupId = checkgroup.getId();
        //添加中间关系表连接
        System.out.println(checkgroupId);

        List<CheckgroupCheckitem> list= new ArrayList<>();
        for (Integer checkitemId : checkitemIds) {
            list.add(new CheckgroupCheckitem(checkgroupId,checkitemId));
        }
        checkGroupItemService.saveBatch(list);


    }

    // 删除
    @Override
    public void deleteById(Integer groupId) {
        /**
         *判断检查组是否被套餐关联，如果有关联，就无法删除检查组
         */

        //创建mybatisPlus条件构造器，采用Lambda表达式，根据检查组ID查询检查组是否被套餐关联
        LambdaQueryWrapper<SetmealCheckgroup> wrapper = new LambdaQueryWrapper<SetmealCheckgroup>();
        wrapper.eq(SetmealCheckgroup::getCheckgroup_id, groupId);
        Integer count = setmealCheckgroupMapper.selectCount(wrapper);

        if (count > 0) {
            BussinessException.error("该检查组被套餐关联，需要先删除检查组和套餐的关联关系以后才能进行删除");
        }else {
            //根据检查组id 删除检查项和检查组的关联关系
            checkGroupItemService.removeByGroupId(groupId);

            //根据检查组id 删除检查项
            checkgroupMapper.deleteById(groupId);
        }

    }

    // 更新
    @Override
    public void update(Checkgroup checkgroup, Integer[] checkitemIds) {
        checkGroupItemService.removeByGroupId(checkgroup.getId());

        List<CheckgroupCheckitem> list = new ArrayList<>();
        for (Integer checkitemId : checkitemIds){
            list.add(new CheckgroupCheckitem(checkgroup.getId(),checkitemId));
        }
        checkGroupItemService.saveBatch(list);

        checkgroupMapper.updateById(checkgroup);
    }

    // 详情(检查组实体需设置检查组id列表)
    @Override
    public Checkgroup findOne(Integer groupId) {
        QueryWrapper<Checkgroup> wrapper = new QueryWrapper<>();
        wrapper.eq("id", groupId);
        Checkgroup selectOne = checkgroupMapper.selectOne(wrapper);
        List<CheckgroupCheckitem> checkgroupCheckitems = checkGroupItemService.listByGroupId(groupId);
        List<Integer> itemIds = checkgroupCheckitems.stream().map(CheckgroupCheckitem::getCheckitemId).collect(Collectors.toList());
        for (CheckgroupCheckitem checkgroupCheckitem : checkgroupCheckitems) {
            itemIds.add(checkgroupCheckitem.getCheckitemId());

        }
        selectOne.setCheckItemIds(itemIds);
        return selectOne;
    }

    // 分页查询
    @Override
    public PageResult<Checkgroup> selectPage(QueryPageBean queryPageBean) {

        Page<Checkgroup> page = queryPageBean.getPage();

        boolean flag = queryPageBean.getQueryString() != null && queryPageBean.getQueryString() != "";

        QueryWrapper<Checkgroup> wrapper = new QueryWrapper<>();
        wrapper.like(flag, "code", queryPageBean.getQueryString())
                .or()
                .like(flag, "name", queryPageBean.getQueryString())
                .or().like(flag, "help_code", queryPageBean.getQueryString());

        Page<Checkgroup> checkgroupPage = checkgroupMapper.selectPage(page, wrapper);
        return new PageResult<>(checkgroupPage);
    }

}
